<?php
namespace app\index\controller;
use think\Db;
class Menu
{
	// 这是通过父亲节点去查找子节点
	public function menuList(){
		$params = $_GET;
		$pId = $params['pId'];
		$data['data'] = Db::table('website_menu')->where('p_id',$pId)->order('sort_tree asc')->select();
		echo json_encode($data);
	}

	// 这是查找滚动的图片的
	public function findBanner(){
		$params = $_GET;
		$data['data'] = Db::table('website_banner')->select();
		echo json_encode($data);
	}
	
	/* 
	* 参数解析
	* 参数1：id：这个是轮播的ID
	* 参数4：pageSize：这个是每次查询的数据条数
	* 参数5：currentPage：这是是查找第几页数据
	*/
	public function findAllBanner(){
		$params = $_GET;
		$flag = true;
		$map;
		if($params['id'] != null || $params['id'] != ""){
			$flag = "false";
			$map['website_banner.id'] = $params['id'];		
		}
		if($flag == "true"){
			$data['data'] = Db::table('website_banner')->order('create_time desc')->limit($params['pageSize']*$params['currentPage'],$params['pageSize'])->select();
			$data['count'] = Db::table('website_banner')->count();
		}else{
			$data['data'] = Db::table('website_banner')->where($map)->order('create_time desc')->limit($params['pageSize']*$params['currentPage'],$params['pageSize'])->select();
			$data['count'] = Db::table('website_banner')->where($map)->count();
		}
		echo json_encode($data);
	}
	
	
	/*
	* 这是删除轮播的方法 
	* 参数解析
	* 参数1：ids：这个是要删除的轮播ID集合格式是（1,2,3）
	*/
	public function deleteBanner(){
		$params = $_GET;
		// 这里是把前端的参数转换成一个数组进行操作
		$arr = explode(',',$params['ids']);
		$data['data'] = Db::table('website_banner')->delete($arr);
		$data['message'] = "删除成功！";
		$data['status'] = "200";
		echo json_encode($data);
  }
  
  
  // 这是对轮播图进行保存
  public function saveBanner(){
  		$params = $_GET;
			$data = [
				'img_url' => $params["img_url"],
				'banner_place'=>$params["banner_place"],
				'valid'=>$params["valid"],
				'creater'=>'1',
				'create_time'=>date("Y/m/d H:m:s")
	   	];
	   	if($params['bannerId']=="" || $params['bannerId']==null){
				$result["data"] = Db::table('website_banner')->insert($data);
			}else{
				$result["data"] = Db::table('website_banner')->where('id', $params['bannerId'])->update($data);
			}
	   
	    $result["status"] = "200";
	    echo  json_encode($result);
  }
  
  // 这是通过ID查找一个轮播
  public function findOneBanner(){
  	$params = $_GET;
  	$banner = $params['id'];
		$data['data'] = Db::table('website_banner')->where('id',$banner)->find();
		echo json_encode($data);
  }

	// 这里是查找所有的菜单
	public function findAllMenu(){
		$result["data"] = Db::table('website_menu')->alias('t1')->join('website_menu t2','t1.p_id = t2.id')->field("t1.*,t2.name as pName")->order('t1.sort_tree asc')->select();
		echo json_encode($result);
	}

	// 这里是进行查找
	public function findMenu(){
		$result["data"] = Db::table('website_menu')->alias('t1')->field("t1.id as p_id,t1.name as pName")->order('sort_tree asc')->select();
		echo json_encode($result);
	}

	// 这里删除指定的导航
	public function deleteMenu(){
		$params = $_GET;
		// 这里是把前端的参数转换成一个数组进行操作
		$arr = explode(',',$params['ids']);
		$data['data'] = Db::table('website_menu')->delete($arr);
		$data['message'] = "删除成功！";
		$data['status'] = "200";
		echo json_encode($data);
	}

	// 这里保存菜单的方法
	public function saveMenu(){
		$params = $_GET;
		$data = [
			'name'    			=> $params["name"],
			'sort_tree'         => $params["sort_tree"],
			'p_id'       		=> $params["p_id"],
			'content'			=> $params["content"],
			'update_time'       => date("Y/m/d H:m:s"),
			'updater'       	=> "1"
		];
		if(isset($params["valid"])){
			$data['valid'] = $params["valid"];
		}else{
			$data['valid'] = "0";
		}
		if(isset($params["url"])){
			$data['url'] = $params["url"];
		}else{
			$data['url'] = "";
		}
	   	if(isset($params["id"])){
			$result["data"] = Db::table('website_menu')->where('id', $params['id'])->update($data);
		}else{
			$result["data"] = Db::table('website_menu')->insert($data);
		}
	   
	    $result["status"] = "200";
		echo  json_encode($result); 
    }
}
